$(function(){
    //banner图片轮播
    $('.flexslider').flexslider({
        directionNav: true,
        pauseOnAction: false,
        slideshowSpeed:4000,
        animationDuration: 2000,
    //slideshow: false,//是否自动播放
    });

    //回到顶部
    //首先将#back-to-top隐藏
    $("#back-to-top").hide();
    //当滚动条的位置处于距顶部100像素以下时，跳转链接出现，否则消失
      $(window).scroll(function(){
      if ($(window).scrollTop()>1200){
      $("#back-to-top").fadeIn(1000);
      }
      else
      {
      $("#back-to-top").fadeOut(800);
      }
      });
      //当点击跳转链接后，回到页面顶部位置
      $("#back-to-top").click(function(){
      $('body,html').animate({scrollTop:0},1000);
      return false;
      });

  //页面滚动后导航悬浮
  var winHeight = $(document).scrollTop();
  $(window).scroll(function() {
      var scrollY = $(document).scrollTop();// 获取垂直滚动的距离，即滚动了多少
    
      if (scrollY > 50){ //如果滚动距离大于550px则隐藏，否则删除隐藏类
        $('.header-top').addClass('fixed');
      } 
      else {
        $('.header-top').removeClass('fixed');
      }      
  });

  /*登陆和注册切换*/
  $('.login-type span').click(function(){
    $(this).addClass('on').siblings().removeClass('on');
    $('.login-tab-box>.login-tab:eq('+$(this).index()+')').show().siblings().hide(); 
  });

  /*登陆和注册切换2*/
  $('.login-register-btn span').click(function(){
    $(this).addClass('on').siblings().removeClass('on');
    $('.login-register-tab>div:eq('+$(this).index()+')').show().siblings().hide(); 
  });

 /*导航下拉tab切换*/
  $('.header-nav2 h2 span').click(function(){
    $(this).addClass('on').siblings().removeClass('on');
    $('.header-nav2-tab>ul:eq('+$(this).index()+')').show().siblings().hide(); 
  })

  /*layer*/
  $(".sign").click(function(){
    $(".layer").css({"display":"block"});
  });   
  $(".layer .close").click(function(){
    $(".layer").css({"display":"none"});
  });
  $(document).bind("click",function(e){ 
    var target = $(e.target); 
    if(target.closest(".login-box,.sign").length == 0){ 
    $(".layer").css({"display":"none"});
    } 
  });

  /*加入购物车后的成功提示*/
  $(".cart-btn").click(function(){
    $(".tip-layer").css({"display":"block"});
  });   
  $(".tip-layer .close").click(function(){
    $(".tip-layer").css({"display":"none"});
  });
  $(document).bind("click",function(e){ 
    var target = $(e.target); 
    if(target.closest(".cart-btn,.tip-box").length == 0){ 
    $(".tip-layer").css({"display":"none"});
    } 
  });
  /*视频点击播放*/
  $(".video-list li").click(function(){
    var videoSrc=$(this).attr("data");
    $(".video-layer").find("embed").attr("src",videoSrc);
    $(".video-layer").css({"display":"block"});
  });   
  $(".video-layer .close").click(function(){
    $(".video-layer").css({"display":"none"});
  });
  $(document).bind("click",function(e){ 
    var target = $(e.target); 
    if(target.closest(".video-box,.video-list li").length == 0){ 
    $(".video-layer").css({"display":"none"});
    } 
  })


   //product 控制显示模式切换
    function getCookie(c_name)
            {
                if (document.cookie.length>0)
                    {
                        c_start=document.cookie.indexOf(c_name + "=")
                        if (c_start!=-1)
                        { 
                            c_start=c_start + c_name.length+1 
                            c_end=document.cookie.indexOf(";",c_start)
                            if (c_end==-1) c_end=document.cookie.length
                            return unescape(document.cookie.substring(c_start,c_end))
                        } 
                    }
                return ""
            }

    $(function(){
        var box=$("#box");
        var list=$("#list");
        var newlist=$("#product-list");

        box.click(function(){
          $(this).attr("class","box-h");
          $(this).next().attr("class","list");
          newlist.removeClass("product-list2");
          document.cookie="product_list_mode=1";
        });

        list.click(function(){
          $(this).attr("class","list-h");
          $(this).prev().attr("class","box");
          newlist.addClass("product-list2");
          document.cookie="product_list_mode=2";
        });

        if (getCookie('product_list_mode')==1) {
          box.attr("class","box-h");
          box.next().attr("class","list");
          newlist.removeClass("product-list2");
        };
        
        if (getCookie('product_list_mode')==2) {
          list.attr("class","list-h");
          list.prev().attr("class","box");
          newlist.addClass("product-list2");
        };
    });


    /*fancybox*/
    $( '[rel=fancybox-thumb]').fancybox({
      showCloseButton:'true',
      showNavArrows:'true',
    });


    /*show-img*/
    $(".show-img").click(function(){
        $(this).toggleClass("show-img-current");
    })

    /*check-more*/
    $(".check-more").click(function(){
         $(this).addClass("hide");
    })

    $(".rate em").click(function(){
         $(this).toggleClass("on");
    })

    /*default-down*/
    $(".default-l span").click(function(){
         $(this).find("i").toggleClass("default-up");
    })

});



/*切换图片集*/
function togglePic(pics,togglePic){//参数：要的切换图片集，要切换的目标图片
  pics.bind("mouseover",function() {
    var toggleImg = $(this).find('img')
    $(this).addClass('active').siblings('li').removeClass('active');
    togglePic.find('img')[0].src = toggleImg[0].src;
  }); 
}
/*箭头切换*/
function  rec(obj){
        var $obj=$(obj);      
        var listpic = $obj.children("ul");
        var picAmount = listpic.children("li").length;
        var Liwidth=listpic.children("li").width();
        var Boxwidth=$obj.width();
        var maxIndexVal = (Math.ceil(picAmount/5)-1)*(-Boxwidth);
        var clicktag = 0;


        listpic.width(picAmount*(Liwidth+25)+"px");

        $obj.siblings(".pright").bind("click",function() {
          if (clicktag == 0) {
            clicktag = 1;
            if(listpic.css("left") != (maxIndexVal+"px")){
              listpic.animate({"left": "-="+Boxwidth+"px"});
            }else{
              listpic.animate({"left": "0px"});
            }
            setTimeout(function () { clicktag = 0 }, 400);//400ms后执行这个函数
          }
        });

        $obj.siblings(".pleft").bind("click",function() {
          if (clicktag == 0) {
            clicktag = 1;
            if(listpic.css("left") != "0px"){
              listpic.animate({"left": "+="+Boxwidth+"px"});
            }else{
              listpic.animate({"left": maxIndexVal+"px"});
            }
            setTimeout(function () { clicktag = 0 }, 400);//400ms后执行这个函数
          }
        });
}

$(function(){
   togglePic($(".small-pic>ul>li"),$(".big-pic"));
   rec($(".small-pic"));
   rec($(".other-scroll"));
   rec($(".rh-scroll"));
});


$(function(){
  $.fn.magnifying = function(){
    var that = $(this),
     $imgCon = that.find('.big-pic'),//正常图片容器
      $Img = $imgCon.find("a").children('img'),//正常图片，还有放大图片集合
       $Drag = that.find('.magnifyingBegin'),//拖动滑动容器
       $show = that.find('.magnifyingShow'),//放大镜显示区域
       $showIMg = $show.find('img'),//放大镜图片
       multiple = $show.width()/$Drag.width();

      

    $imgCon.mousemove(function(e){

      var  src=$Img.attr("src");
      $showIMg.attr("src",src);

      $Drag.css('display','block');
      $show.css('display','block');
        //获取坐标的两种方法
        // var iX = e.clientX - this.offsetLeft - $Drag.width()/2,
        //  iY = e.clientY - this.offsetTop - $Drag.height()/2, 
          var iX = e.pageX - $(this).offset().left - $Drag.width()/2,
          iY = e.pageY - $(this).offset().top - $Drag.height()/2, 
          MaxX = $imgCon.width()-$Drag.width(),
          MaxY = $imgCon.height()-$Drag.height();
        
            /*这一部分可代替下面部分，判断最大最小值
        var DX = iX < MaxX ? iX > 0 ? iX : 0 : MaxX,
          DY = iY < MaxY ? iY > 0 ? iY : 0 : MaxY;
        $Drag.css({left:DX+'px',top:DY+'px'});        
        $showIMg.css({marginLeft:-3*DX+'px',marginTop:-3*DY+'px'});*/

        iX = iX > 0 ? iX : 0;
        iX = iX < MaxX ? iX : MaxX;
        iY = iY > 0 ? iY : 0;
        iY = iY < MaxY ? iY : MaxY; 
        $Drag.css({left:iX+'px',top:iY+'px'}); 
        $showIMg.css({marginLeft:-multiple*iX+'px',marginTop:-multiple*iY+'px'});
        //return false;
    });
    $imgCon.mouseout(function(){
        $Drag.css('display','none');
      $show.css('display','none');
    });
  }

  $("#fangdajing").magnifying();


});
